#include "tft.h"
#include "bitbind.h"
#include "spi.h"
#include "delay.h"

void tft_write_comm(u16 cmd);
void tft_write_data(u16 tem_data);
void tft_wr_reg(u16 index,u16 congfig_temp);

void tft_cs(u8 cs)
{
	if(cs)
		TFT_CS=1;
	else
		TFT_CS=0;
}

void BlockWrite(unsigned int Xstart,unsigned int Xend,unsigned int Ystart,unsigned int Yend) 
{

	tft_wr_reg(0x0020, Ystart);
	tft_wr_reg(0x0021, Xstart);
	tft_wr_reg(0x0052, Xstart);
	tft_wr_reg(0x0053, Xend);
	tft_wr_reg(0x0050, Ystart);	
	tft_wr_reg(0x0051, Yend);

	tft_write_comm(0x22);
}

void Lcd_ColorBox(u16 xStart,u16 yStart,u16 xLong,u16 yLong,u16 Color)
{
	u32 i;
	BlockWrite(xStart,xStart+xLong-1,yStart,yStart+yLong-1);
	tft_cs(0);
	spi_write_byte(SPI1, 0x72);
	for (i=0; i<xLong*yLong; i++)
	{
		spi_write_byte(SPI1, Color>>8);i=i;i=i;i=i;i=i; i=i;
		spi_write_byte(SPI1, Color);   i=i;i=i;i=i;i=i; i=i;i=i;i=i;i=i; i=i;i=i;i=i;i=i; i=i;i=i;i=i;
	}
	tft_cs(1);
}

void tft_init(void)
{	
	GPIO_InitTypeDef  GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  GPIO_Init(GPIOA, &GPIO_InitStructure);
	tft_cs(1);
	
	GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
	GPIO_Init(GPIOE, &GPIO_InitStructure);
	TFT_LIGHT=0;
		
	spi_init(SPI1);

	delay_us(2250);

	tft_write_comm(0x0000);tft_write_data(0x0000);
	tft_write_comm(0x0000);tft_write_data(0x0000);
	tft_write_comm(0x0060);tft_write_data(0x2700);
	tft_write_comm(0x0090);tft_write_data(0x001a);
	tft_write_comm(0x0008);tft_write_data(0x0808);
	tft_write_comm(0x0030);tft_write_data(0x0505);
	tft_write_comm(0x0031);tft_write_data(0x8a10);
	tft_write_comm(0x0032);tft_write_data(0x0907);
	tft_write_comm(0x0033);tft_write_data(0x0308);
	tft_write_comm(0x0034);tft_write_data(0x0000);
	tft_write_comm(0x0035);tft_write_data(0x0803);
	tft_write_comm(0x0036);tft_write_data(0x8709);
	tft_write_comm(0x0037);tft_write_data(0x100a);
	tft_write_comm(0x0038);tft_write_data(0x0505);
	tft_write_comm(0x0039);tft_write_data(0x3333);
	tft_write_comm(0x000A);tft_write_data(0x0008);
	tft_write_comm(0x0010);tft_write_data(0x0730);
	tft_write_comm(0x0011);tft_write_data(0x0237);
	tft_write_comm(0x0012);tft_write_data(0x0198);
	tft_write_comm(0x000E);tft_write_data(0x0020);
	tft_write_comm(0x0013);tft_write_data(0x1500);
	tft_write_comm(0x002A);tft_write_data(0x0075);
	tft_write_comm(0x0012);tft_write_data(0x01Bc);  

	delay_ms(2);

	//display setting
	tft_write_comm(0x0000);tft_write_data(0x0001);
	tft_write_comm(0x0001);tft_write_data(0x0000);
	tft_write_comm(0x0002);tft_write_data(0x0700);
	tft_write_comm(0x0003);tft_write_data(0x1038);
	tft_write_comm(0x0004);tft_write_data(0x0000);
	tft_write_comm(0x0009);tft_write_data(0x0001);
	tft_write_comm(0x000A);tft_write_data(0x0008);//turn on framesignal 
	tft_write_comm(0x000C);tft_write_data(0x0000);//Internal clock operations
	tft_write_comm(0x000D);tft_write_data(0x0000);
	tft_write_comm(0x000E);tft_write_data(0x0030);
	tft_write_comm(0x000F);tft_write_data(0x0002);

	tft_write_comm(0x0050);tft_write_data(0x0000);
	tft_write_comm(0x0051);tft_write_data(0x00Ef);
	tft_write_comm(0x0052);tft_write_data(0x0000);
	tft_write_comm(0x0053);tft_write_data(0x013F);
	tft_write_comm(0x0061);tft_write_data(0x0000);
	tft_write_comm(0x0080);tft_write_data(0x0000);
	tft_write_comm(0x0081);tft_write_data(0x0000);
	tft_write_comm(0x0082);tft_write_data(0x0000);
	tft_write_comm(0x006A);tft_write_data(0x0000);
	tft_write_comm(0x0092);tft_write_data(0x0200);
	tft_write_comm(0x0093);tft_write_data(0x0202);

	tft_write_comm(0x0020);tft_write_data(0x0000);
	tft_write_comm(0x0021);tft_write_data(0x0000);
	tft_write_comm(0x0007);tft_write_data(0x0100); 


	delay_ms(2);
	tft_write_comm(0x0022); 
	delay_ms(2);
	TFT_LIGHT=1;
}



void tft_write_comm(u16 cmd)
{			
//	u8 i=0;
//	DIS_INT();
	tft_cs(0);
//	delay_us(1);
//	wait_us(1);
	
	//SPI_I2S_SendData(SPI1, 0x70);
	spi_write_byte(SPI1, 0x70);
	//TFT_CS=0;

	//SPI_I2S_SendData(SPI1, (u8)(cmd>>8));
	spi_write_byte(SPI1, (u8)(cmd>>8));

	//SPI_I2S_SendData(SPI1, (u8)cmd);
	spi_write_byte(SPI1, (u8)(cmd));

	tft_cs(1);
//	delay_us(1);
//	wait_us(1);
	
//	EN_INT();
//	delay_us(1);
}

void tft_write_data(u16 tem_data)
{			
//	u8 i=0;
	tft_cs(0);
//	delay_us(1);
	//SPI_I2S_SendData(SPI1, 0x72);
	spi_write_byte(SPI1, 0x72);
	//SPI_I2S_SendData(SPI1, (u8)(tem_data>>8));
	spi_write_byte(SPI1, (u8)(tem_data>>8));
	//TFT_CS=0;
	//SPI_I2S_SendData(SPI1, (u8)tem_data);
	spi_write_byte(SPI1, (u8)(tem_data));
	tft_cs(1);
//	delay_us(1);
//	delay_us(1);
}

void tft_wr_reg(u16 index,u16 congfig_temp)
{
	tft_write_comm(index);
	tft_write_data(congfig_temp);
}

void tft_draw_pixel(u16 x, u16 y, u16 color)
{
	tft_wr_reg(0x0020, y);
	tft_wr_reg(0x0021, x);
	tft_write_comm(0x22);
	tft_write_data(color);
}





